<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function index(){
        
        echo $this->display();
    }
    public function register(){
    	//添加用户
    	//用户名是否存在，口令是否正确，口令加密
    	$userModel=D('Users');

    	if($userModel->create())
        {
        	if ($_FILES['photo']['error']==0) {//判断文件是否成功
                $fileName=$this->img();
                //$photoinfo=$this->upload();//将临时文件保存到指定位置
                $userModel->photo=$fileName;//获取并设置文件名称     
         }
            

                  if ($userModel->add()) {
                    $this->success('注册用户成功！',U('Admin/Index/users'));/*添加成功后返回users页面*/
                  }else{
                    $this->error('注册用户失败！');
                  }
 
                }else{
                    $this->error($userModel->getError());
                }

    }
    //文件上传
    public function upload(){
      $upload = new \Think\Upload();// 实例化上传类
      $upload->maxSize = 3145728 ;// 设置附件上传大小
      $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
      $upload->rootPath = './Public/Uploads/photo/'; // 设置附件上传根目录
      //$upload->savePath = ''; // 设置附件上传（子）目录
      $upload->autoSub=false;
      // 上传文件
      $info = $upload->upload();
      if(!$info) {// 上传错误提示错误信息
      $this->error($upload->getError());
      }else{// 上传成功
      //$this->success('上传成功！');
        return $info;
      }
    }



    function img(){
        
       $photoinfo=$this->upload();

       $filePath='./Public/Uploads/photo/';
       $oldFileName=$photoinfo['photo']['savename'];
       $oldFilFulleName=$filePath."\\".$oldFileName;
       $newFileName='thumb_'.$oldFileName;
       $newFilFulleName=$filePath."\\".$newFileName;

       $imgFile=$photoinfo['photo']['rootPath'].'\\'.$photoinfo['photo']['savename'];
       $image=new \Think\Image();
       $image->open($oldFilFulleName);
       $image->thumb(100,100)->save($newFilFulleName);
       return $newFileName;


    }












}